package pers.qianyu.month_202103.date_20210328;

import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 5713. 字符串中不同整数的数目
 * https://leetcode-cn.com/problems/number-of-different-integers-in-a-string/
 *
 * @author mizzle rain
 * @date 2021-03-28 10:52
 */
public class NumDifferentIntegers {
    public int numDifferentIntegers(String word) {
        int n = word.length();
        if (n == 0) return 0;
        char[] arr = word.toCharArray();
        for (int i = 0; i < n; i++) {
            if (arr[i] < '0' || arr[i] > '9') {
                arr[i] = ' ';
            }
        }
        String str = new String(arr);
        String[] s = str.split(" ");
        Set<String> set = Arrays.stream(s)
                .filter(s1 -> s1.length() > 0)
                .map(s1 -> {
                    char[] chars = s1.toCharArray();
                    int len = chars.length;
                    int i = 0;
                    while (i < len && chars[i] == '0') i++;
                    if (i == len) return "0";
                    String res = new String(chars, i, len - i);
                    return res.length() == 0 ? "0" : res;
                })
                .collect(Collectors.toSet());
        return set.size();
    }

    @Test
    public void test1() {
        int r = new NumDifferentIntegers().numDifferentIntegers("a123bc34d8ef34");
        Assert.assertEquals(3, r);
    }

    @Test
    public void test2() {
        int r = new NumDifferentIntegers().numDifferentIntegers("leet1234code234");
        Assert.assertEquals(2, r);
    }

    @Test
    public void test3() {
        int r = new NumDifferentIntegers().numDifferentIntegers("a1b01c001");
        Assert.assertEquals(1, r);
    }

    @Test
    public void test4() {
        int r = new NumDifferentIntegers().numDifferentIntegers("167278959591294");
        Assert.assertEquals(1, r);
    }

    @Test
    public void test5() {
        int r = new NumDifferentIntegers().numDifferentIntegers("0a0");
        Assert.assertEquals(1, r);
    }

    @Test
    public void test6() {
        String s1 = "000123";
        char[] chars = s1.toCharArray();
        int i = 0;
        while (chars[i] == '0') i++;
        System.out.println(new String(chars, i, chars.length - i));
    }
}
